package com.technologys.ctrl;

import java.util.UUID;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class LoginCtrl {

	private static final Logger log = LoggerFactory.getLogger(LoginCtrl.class);
	
	@RequestMapping(value="login", method=RequestMethod.POST)
	public String login(@RequestParam(required=true) String username, @RequestParam(required=true) String password) {
		UsernamePasswordToken token = new UsernamePasswordToken(
				username, password);
		try {
			SecurityUtils.getSubject().login(token);
		} catch (Exception e) {
			log.info("Error authenticating.{}", e.getMessage());
			return "login";
		}
		return "index";
	}
	
	@RequestMapping(value="login", method=RequestMethod.GET)
	public String login() {
		return "login";
	}
	
	public static void main(String[] args) {
		String uuid = UUID.randomUUID().toString();
		log.info(uuid);
	}
}